import configparser
import mysql.connector


class Database(object):

    def connect(self):
        parser = configparser.ConfigParser()
        parser.read('config.ini')
        db_config = dict(parser.items('DB'))
        self.connection = mysql.connector.connect(**db_config)
        self.cursor = self.connection.cursor()

    def save_employee(self, employee):
        self.connect()

        sql = "INSERT INTO employee (code, name) VALUES (%s, %s)"
        val = (employee.code, employee.name)
        self.cursor.execute(sql, val)
        self.connection.commit()

        self.close()

    def query_employee(self):
        sql = 'select * from employee'
        self.connect()
        self.cursor.execute(sql)
        result = self.cursor.fetchall()
        self.close()
        return result

    def close(self):
        self.cursor.close()
        self.connection.close()
